import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Delete from '#cpt/temp/Delete'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 			= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 		= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '分组名称',		name: 'gp_name' },
			{ label: '分组代码',		name: 'gp_code' },
			{ label: '分组类型',		name: 'gp_type',	type: 'select', loading:true, idStr:'code' },
		],
		// 提交表单列表
		formData : [
			{ label: '分组名称',		name: 'gp_name', 	required:true, half:true },
			{ label: '分组代码',		name: 'gp_code', 	required:true, half:true },
			{ label: '分组类型',		name: 'gp_type',	type: 'select', required:true, half:true, idStr:'code' },
			{ label: '排序',		name: 'sort', 		required:true, type:'number', half:true },
			{ label: '描述',		name: 'gp_desc',		full:true,         type:'textarea' },
		]
	}
	
	model = {}
	
	componentDidMount(){
		this.getSelect()
		
		this.fetch()
	}
	// 获取下拉数据
	getSelect = async () => {
		const { forms, formData } = this.state
		$cache.then( async f => {
			// 上级菜单
			$fn.setRows([ forms, formData ], 'gp_type', await f.getDicCache({ code: 500 } ))
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'groups/index')
	
	// 表单
	cols = [
		{ title: 'ID',			dataIndex: 'id', 				width:50 },
		{ title: '分组名称', 	dataIndex: 'gp_name', 			width:150 },
		{ title: '分组代码', 	dataIndex: 'gp_code', 			width:150 },
		{ title: '分组类型', 	dataIndex: 'gp_type', 			width:150 },
		{ title: '所属分公司', 	dataIndex: 'company_name', 		width:150 },
		{ title: '创建时间', 	dataIndex: 'created_at', 		...$fn.table.time },
		TableControls.controls.call(this, { width: 200})
	]
	
	// 表格操作按钮
	table_controls = (text,{ uuid }) => {
		return [
			TableControls.edit.call(this, 'groups/info', { uuid }),				// 编辑
			this.onDel([ uuid ]),												// 删除
			Controls.pushTo.call(this, '权限关联', `permissions?route=groups&uuid=${uuid}` )
		]
	}
	
	// 操作按钮
	controls = () => {
		return [
			Controls.add.call(this),			// 添加
			this.onDel(),						// 删除
		]
	}
	
	// 删除
	onDel = uuid => Delete.call(this, 'groups/delete', { uuid, isKeys: !uuid })
	
	render(){
		return (
			<>
				<Page title='分组列表' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ api: 'groups/add', editApi:'groups/edit', getSubmit:()=>{
					const { comp_id, company } = $fn.getUser()
					return { comp_id, company }
				} })}
			</>
		)
	}
}

export default Index